import categoryApi from "../../api/category";

const initState = () => ({
    category1Id: undefined,
    category2Id: undefined,
    category3Id: undefined,
    category1List: [],
    category2List: [],
    category3List: [],
})


const state = initState()

const actions = {
    async getCategory1List() {
        try {
            let res = await categoryApi.getCategory1List()
            this.category1List = res;
        } catch (error) {
            this.$message.error('获取1级分类数据失败');
        }
    },
    async getCategory2List() {
        try {
            this.category2Id = undefined;
            this.category3Id = undefined;
            this.category2List = [];
            this.category3List = [];
            let res = await categoryApi.getCategory2List(this.category1Id);
            this.category2List = res;
        } catch (error) {
            this.$message.error('获取2级分类数据失败');
        }
    },
    async getCategory3List() {
        try {
            this.category3Id = undefined;
            this.category3List = [];
            let res = await categoryApi.getCategory3List(this.category2Id);
            this.category3List = res;
        } catch (error) {

            this.$message.error('获取3级分类数据失败');
        }
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}